<script setup lang="ts">
import { getgoodsGuessLikAPI } from '@/services/home'
import type { PageParams } from '@/types/global'
import type { GuessItem } from '@/types/home'
import { onMounted, ref } from 'vue'
const guessLit = ref<GuessItem[]>([])
const pageParams: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}
const finish = ref(false)

const getHomeGuessData = async () => {
  if (finish.value) {
    return uni.showToast({
      icon: 'none',
      title: '没有更多数据了~',
    })
  }
  const res = await getgoodsGuessLikAPI(pageParams)
  console.log(res)
  // 数据追加
  guessLit.value.push(...res.result.items)
  if (pageParams.page < res.result.pages) {
    pageParams.page++
  } else {
    finish.value = true
  }
}
// 数据重置方法
const resetData = () => {
  pageParams.page = 1
  guessLit.value = []
  finish.value = false
}
// 暴露出去
defineExpose({
  getMore: getHomeGuessData,
  resetData,
})
onMounted(() => {
  getHomeGuessData()
})
</script>

<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
  <view class="guess">
    <navigator
      class="guess-item"
      v-for="item in guessLit"
      :key="item.id"
      :url="`/pages/goods/goods?id=${item.id}`"
    >
      <image class="image" mode="aspectFill" :src="item.picture"></image>
      <view class="name"> {{ item.name }} </view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{ item.price }}</text>
      </view>
    </navigator>
  </view>
  <view class="loading-text"> {{ finish ? '没有更多数据了' : '正在加载中' }} </view>
</template>

<style lang="scss">
@import './style//XtxGuess.scss';
:host {
  display: block;
}
/* 分类标题 */
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  padding: 36rpx 0 40rpx;
  font-size: 32rpx;
  color: #262626;
  .text {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;

    &::before,
    &::after {
      content: '';
      width: 20rpx;
      height: 20rpx;
      background-image: url(@/static/images/bubble.png);
      background-size: contain;
      margin: 0 10rpx;
    }
  }
}
/* 猜你喜欢 */
// 加载提示文字
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}
</style>
